﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CMS6Tools.Common
{
    public class ComponentUsage
    {
        //protected FeatureServiceReference.AuthenticationSoapHeader NewAuthenticationSoapHeader(Client client)
        //{
        //    return new FeatureServiceReference.AuthenticationSoapHeader()
        //    {
        //        ClientIdentity = client.Identifier.ToString("N"),
        //        UserName = WebUtilities.Cms6AuthenticationHeaderUserName,
        //        Password = client.ServicePassword
        //    };
        //}
        //protected FeatureServiceReference.FeatureWebServiceSoapClient NewWebServiceSoapClient(Client client)
        //{
        //    var serviceBaseUrl = client.LiveSiteUrl;
        //    var serviceFullURL = serviceBaseUrl.TrimEnd('/') + "/" + "ws/FeatureWebService.asmx";

        //    var binding = ServiceHelper.CreateDefaultBinding(serviceFullURL);
        //    var endpointAddress = new EndpointAddress(serviceFullURL);
        //    return new FeatureServiceReference.FeatureWebServiceSoapClient(binding, endpointAddress);
        //}
        //public void GetInfo(int? count, int? ignore, bool? xml, bool? byc)
        //{
        //    xml = xml ?? true;
        //    byc = byc ?? false;

        //    var clientService = ObjectContainerManager.Container.Get<IClientService>();
        //    var allClients = clientService.FindAllClients().ToList();
        //    if (ignore.HasValue)
        //        allClients = allClients.Skip(ignore.Value).ToList();
        //    if (count.HasValue)
        //        allClients = allClients.Take(count.Value).ToList();

        //    var allClientFeatureInfos = new List<ClientFeatureInfo>();
        //    foreach (var client in allClients)
        //    {
        //        var clientFeatureInfo = new ClientFeatureInfo()
        //        {
        //            ClientID = client.ID.ToString(),
        //            ClientIdentifier = client.Identifier.ToString("N"),
        //            ClientName = client.Name,
        //            ClientURL = client.LiveSiteUrl,//string.IsNullOrEmpty(client.LiveSiteUrl) ? client.DevSiteUrl : client.LiveSiteUrl,
        //            ServicePassword = client.ServicePassword,
        //            Type = EnumDisplayAttribute.GetDisplayText(client.Type)
        //        };
        //        try
        //        {
        //            using (var featureService = NewWebServiceSoapClient(clientFeatureInfo))
        //            {
        //                var allFeatures = featureService.FindAllFeatureModels(NewAuthenticationSoapHeader(clientFeatureInfo));
        //                clientFeatureInfo.Features = allFeatures;
        //            }
        //        }
        //        catch (Exception e)
        //        {
        //            clientFeatureInfo.Reason = "wrong";
        //        }
        //        allClientFeatureInfos.Add(clientFeatureInfo);
        //    }

        //    var dataStr = string.Empty;
        //    var headerValue = "";
        //    var contentType = "";

        //    if (xml.Value)
        //    {
        //        dataStr = ObjectXmlSerializer.Serialize(allClientFeatureInfos);
        //        headerValue = "attachment;filename=\"info.xml\"";
        //        contentType = "application/xml";
        //    }
        //    else
        //    {
        //        if (byc.Value)
        //        {
        //            var byFeatureInfo = allClientFeatureInfos.Where(c => c.Features.Any()).SelectMany(c => c.Features.Select(d => new { c.ClientID, c.ClientIdentifier, c.ClientName, c.ClientURL, c.Type, c.Reason, d.DisplayName, d.Actived }))
        //                                 .GroupBy(c => c.DisplayName)
        //                                 .Select(c => new { FeatureName = c.Key, ActivedCount = c.Count(d => d.Actived), InActivedCount = c.Count(d => !d.Actived) })
        //                                 .ToList();
        //            var firstRow = new StringBuilder();
        //            var fields = new StringBuilder();
        //            var carriageReturn = "\r\n";
        //            firstRow.Append("Feature Name, Actived, Inactived");
        //            foreach (var feature in byFeatureInfo)
        //            {
        //                fields.AppendFormat("{0},", feature.FeatureName);
        //                fields.AppendFormat("{0},", feature.ActivedCount);
        //                fields.AppendFormat("{0}", feature.InActivedCount);
        //                fields.Append(carriageReturn);
        //            }
        //            dataStr = firstRow.ToString() + carriageReturn + fields.ToString();
        //            headerValue = "attachment;filename=\"info_byfeature.csv\"";
        //        }
        //        else
        //        {
        //            var firstRow = new StringBuilder();
        //            var fields = new StringBuilder();
        //            var carriageReturn = "\r\n";
        //            firstRow.Append("Client Name, Client URL, Client Identifier, Client Type, Feature Name, Feature Actived, Reason");
        //            foreach (var clientFeature in allClientFeatureInfos)
        //            {
        //                if (clientFeature.Features != null && clientFeature.Features.Any())
        //                {
        //                    foreach (var feature in clientFeature.Features)
        //                    {
        //                        fields.AppendFormat("\"{0}\",", clientFeature.ClientName);
        //                        fields.AppendFormat("{0},", clientFeature.ClientURL);
        //                        fields.AppendFormat("{0},", clientFeature.ClientIdentifier);
        //                        fields.AppendFormat("\"{0}\",", clientFeature.Type);
        //                        fields.AppendFormat("\"{0}\",", feature.DisplayName);
        //                        fields.AppendFormat("{0}", feature.Actived ? "Y" : "N");
        //                        fields.Append(carriageReturn);
        //                    }
        //                }
        //                else
        //                {
        //                    fields.AppendFormat("\"{0}\",", clientFeature.ClientName);
        //                    fields.AppendFormat("{0},", clientFeature.ClientURL);
        //                    fields.AppendFormat("{0},", clientFeature.ClientIdentifier);
        //                    fields.AppendFormat("\"{0}\",", clientFeature.Type);
        //                    fields.AppendFormat(",,{0}", "wrong");
        //                    fields.Append(carriageReturn);
        //                }
        //            }
        //            dataStr = firstRow.ToString() + carriageReturn + fields.ToString();
        //            headerValue = "attachment;filename=\"info.csv\"";
        //        }

        //        contentType = "text/plain";
        //    }

        //    Response.Clear();

        //    Response.ContentType = contentType;
        //    Response.AddHeader("Content-Disposition", headerValue);
        //    Response.Buffer = true;
        //    Response.Write(dataStr);
        //    Response.Flush();
        //    Response.End();
        //}

        //public class ClientFeatureInfo
        //{
        //    public ClientFeatureInfo()
        //    {
        //        Features = new FeatureServiceReference.WebServiceFeatureModel[0];
        //    }

        //    public string ClientID { get; set; }
        //    public string ClientIdentifier { get; set; }
        //    public string ClientName { get; set; }
        //    public string ClientURL { get; set; }
        //    public string Reason { get; set; }
        //    public string Type { get; set; }
        //    public string ServicePassword { get; set; }
        //    public FeatureServiceReference.WebServiceFeatureModel[] Features { get; set; }
        //}
    }
}
